<template>
  <div class="login-container" :style="{background: `url(${backgroundUrl}) no-repeat`, 'background-size': '100% 100%'}">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="container">
        <div class="icon-box">
          <div class="icon">
            <img class="img" width="180" height="180" src="@/assets/logo/mooc_logo.png" alt="">
          </div>
          <img class="img1" width="20" height="20" src="@/assets/img/crawler.png" alt="">
          <img class="img2" width="20" height="20" src="@/assets/img/line.png" alt="">
          <img class="img3" width="20" height="20" src="@/assets/img/bar.png" alt="">
          <img class="img4" width="20" height="20" src="@/assets/img/setting.png" alt="">
        </div>
        <div class="login-box">
          <div class="operation">
            <div class="login-oper">登录</div>
            <div class="register-oper" @click="goRegister">注册</div>
          </div>
          <el-form-item prop="username" class="username">
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入内容"
              type="text"
              auto-complete="on"
            >
              <span slot="prefix" class="svg-container">
                <svg-icon icon-class="user" />
              </span>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              ref="password"
              v-model="loginForm.password"
              placeholder="请输入内容"
              type="password"
              auto-complete="on"
            >
              <span slot="prefix" class="show-pwd">
                <svg-icon icon-class="password" />
              </span>
            </el-input>
          </el-form-item>
          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { set } from 'js-cookie'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      backgroundUrl: require('@/assets/img/background.jpeg')
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          const res = await this.$api.login(this.loginForm)
          if (res && res.code === 200 && +res.data.busiCode === 1) {
            set('token', res.data.accessToken)
            set('refreshToken', res.data.refreshToken)
            localStorage.setItem('userinfo', JSON.stringify(res.data.userInfo))
            this.$router.push({
              path: '/home'
            })
          }
        }
      })
    },
    goRegister() {
      this.$router.push('/register')
    }
  }
}
</script>

<style lang="scss" scoped>

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.login-container {
  .container{
    caret-color: transparent;
  }
}
</style>

<style lang="scss" scoped>
@import '@/styles/mixin.scss';

$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
@keyframes rotate{
  from{transform: rotate(0deg)}
  to{transform: rotate(359deg)}
}
.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
}
.container{
  display: flex;
  justify-content: space-around;
  align-items: center;
  width: 800px;
  @include boxCenter;
  .icon-box{
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 300px;
    height: 300px;
    border-radius: 50%;
    border: 2px dashed #9fe7f8;
    .img1, .img2, .img3, .img4{
      position: absolute;
      opacity: 0.8;
    }
    .img1{
      top: -10px;
    }
    .img4{
      bottom: -10px;
    }
    .img3{
      left: -10px;
    }
    .img2{
      right: -10px;
    }
  }
  .icon{
    position: relative;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    text-align: center;
    background: #4d97f8;
    opacity: 0.9;
    border: 6px solid #7fc7f8;
    .img{
      padding-top: 20px;
      animation: rotate 12s linear infinite;
    }
  }
  .login-box{
    width: 275px;
    height: 330px;
    background: #deecf6;
    padding: 20px 20px 10px;
    @include borderShadow(8px);
    .operation {
      display: flex;
      justify-content: space-between;
      padding-bottom: 10px;
      color: #3e92c9;
      border-bottom: 1px solid #c3cbd1;
      .register-oper{
        cursor: pointer;
        color: #a3a3a3;
      }
    }
    .username{
      margin-top: 30px;
    }
  }
}
</style>
